package com.sena.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sena.base.BasePage;
import com.sena.domain.SenaBlackRegexConfig;
import com.sena.domain.SenaClientSite;
import com.sena.domain.SenaSuffixConfig;
import com.sena.domain.SenaWhiteRegexConfig;
import com.sena.enums.ResultCodeEnum;
import com.sena.exception.ThreadRunTimeException;
import com.sena.mapper.SenaBlackRegexConfigMapper;
import com.sena.mapper.SenaClientSiteMapper;
import com.sena.mapper.SenaSuffixConfigMapper;
import com.sena.mapper.SenaWhiteRegexConfigMapper;
import com.sena.search.*;
import com.sena.service.SenaClientSiteService;
import com.sena.utils.AssertUtils;
import com.sena.vo.RequestBlackList;
import com.sena.vo.RequestSiteList;
import com.sena.vo.RequestSuffixList;
import com.sena.vo.RequestWhiteList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

@Service
@Slf4j
public class SenaClientSiteServiceImp implements SenaClientSiteService {


    @Autowired
    private SenaClientSiteMapper senaClientSiteMapper;


    @Autowired
    private SenaSuffixConfigMapper senaSuffixConfigMapper;
    @Autowired
    private SenaBlackRegexConfigMapper senaBlackRegexConfigMapper;
    @Autowired
    private SenaWhiteRegexConfigMapper senaWhiteRegexConfigMapper;

    @Override
    public int addClientSite(SenaClientSite senaClientSite) {
        return senaClientSiteMapper.insertSelective(senaClientSite);
    }

    @Override
    @Transactional
    public int updateClientSite(SenaClientSite senaClientSite) {
        return senaClientSiteMapper.updateByPrimaryKeySelective(senaClientSite);
    }

    @Override
    public int deleteClientSite(int id) {
        return senaClientSiteMapper.deleteByPrimaryKey(id);
    }


    @Override
    public SenaClientSite selectByPrimaryKey(int id) {
        return senaClientSiteMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页插叙
     *
     * @param request
     * @return
     */
    @Override
    public BasePage<SenaClientSite> listByPage(RequestSiteList request) {
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<SenaClientSite> list = senaClientSiteMapper.listByPage(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<SenaClientSite>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addSenaBlackRegexConfig(SenaBlackRegexConfig senaBlackRegexConfig) {

        SenaClientSite site = senaClientSiteMapper.selectByPrimaryKey(senaBlackRegexConfig.getSiteId());
        if (site != null) {
            senaBlackRegexConfig.setSiteName(site.getClientName());
            return senaBlackRegexConfigMapper.insert(senaBlackRegexConfig);
        } else {
            throw new ThreadRunTimeException(ResultCodeEnum.DATA_NOT_EXIST);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addSenaWhiteRegexConfig(SenaWhiteRegexConfig senaWhiteRegexConfig) {
        SenaClientSite site = senaClientSiteMapper.selectByPrimaryKey(senaWhiteRegexConfig.getSiteId());
        if (site != null) {
            senaWhiteRegexConfig.setSiteName(site.getClientName());
            return senaWhiteRegexConfigMapper.insert(senaWhiteRegexConfig);
        } else {
            throw new ThreadRunTimeException(ResultCodeEnum.DATA_NOT_EXIST);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addSenaSuffixConfig(SenaSuffixConfig senaSuffixConfig) {
        SenaClientSite site = senaClientSiteMapper.selectByPrimaryKey(senaSuffixConfig.getSiteId());
        if (site != null) {
            senaSuffixConfig.setSiteName(site.getClientName());
            return senaSuffixConfigMapper.insert(senaSuffixConfig);
        } else {
            throw new ThreadRunTimeException(ResultCodeEnum.DATA_NOT_EXIST);
        }

    }


    @Override
    public BasePage<SenaBlackRegexConfig> listByPageBlack(RequestBlackList request) {
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<SenaBlackRegexConfig> list = senaBlackRegexConfigMapper.listByPageBlack(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<SenaBlackRegexConfig>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }

    @Override
    public BasePage<SenaWhiteRegexConfig> listByPageWhite(RequestWhiteList request) {
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<SenaWhiteRegexConfig> list = senaWhiteRegexConfigMapper.listByPageWhite(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<SenaWhiteRegexConfig>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }

    @Override
    public BasePage<SenaSuffixConfig> listByPageSuffix(RequestSuffixList request) {
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<SenaSuffixConfig> list = senaSuffixConfigMapper.listByPageSuffix(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<SenaSuffixConfig>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }


    @Override
    public int deleteBlack(int id) {
        return senaBlackRegexConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteWhite(int id) {
        return senaWhiteRegexConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteSuffix(int id) {
        return senaSuffixConfigMapper.deleteByPrimaryKey(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSenaBlackRegexConfig(SenaBlackRegexConfig senaBlackRegexConfig) {

        return senaBlackRegexConfigMapper.updateByPrimaryKeySelective(senaBlackRegexConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSenaWhiteRegexConfig(SenaWhiteRegexConfig senaWhiteRegexConfig) {
        return senaWhiteRegexConfigMapper.updateByPrimaryKeySelective(senaWhiteRegexConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSenaSuffixConfig(SenaSuffixConfig senaSuffixConfig) {
        return senaSuffixConfigMapper.updateByPrimaryKeySelective(senaSuffixConfig);

    }


    @Override
    public SenaBlackRegexConfig selectBlack(int id) {
        return senaBlackRegexConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public SenaWhiteRegexConfig selectWhite(int id) {
        return senaWhiteRegexConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public SenaSuffixConfig selectSuffix(int id) {
        return senaSuffixConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void start() {
        List<SenaClientSite> siteList = senaClientSiteMapper.selectAll();
        siteList.forEach(e -> {
            List<String> suffixList = senaSuffixConfigMapper.selectBySiteId(e.getId());
            List<String> whiteList = senaWhiteRegexConfigMapper.selectBySiteId(e.getId());
            List<String> blackList = senaBlackRegexConfigMapper.selectBySiteId(e.getId());
            if (e.getLinkType() == 1) {
                new Thread(() -> {
                    SearchCommonLink searchCommonLink = new SearchCommonLink();
                    searchCommonLink.SearchCommonLinkYunTask(e, new HashSet<>(), blackList, whiteList, suffixList);
                }).start();
                e.setCrawlingState(1);
                senaClientSiteMapper.updateByPrimaryKeySelective(e);
            } else if (e.getLinkType() == 3) {
                new Thread(() -> {
                    SearchDeathLink searchDeathLink = new SearchDeathLink();
                    searchDeathLink.SearchDeathLinkTask(e, new HashSet<>(), blackList, whiteList, suffixList);
                }).start();
                e.setCrawlingState(1);
                senaClientSiteMapper.updateByPrimaryKeySelective(e);
            } else if (e.getLinkType() == 4) {
                new Thread(() -> {
                    SearchFileLink searchFileLink = new SearchFileLink();
                    searchFileLink.SearchSearchFileLinkTask(e, blackList, whiteList, suffixList);
                }).start();
                e.setCrawlingState(1);
                senaClientSiteMapper.updateByPrimaryKeySelective(e);
            } else if (e.getLinkType() == 5) {
                new Thread(() -> {
                    SearchHiddenLink searchHiddenLink = new SearchHiddenLink();
                    searchHiddenLink.searchHiddenLinkTask(e, blackList, whiteList, suffixList);
                }).start();
                e.setCrawlingState(1);
                senaClientSiteMapper.updateByPrimaryKeySelective(e);
            } else if (e.getLinkType() == 2) {
                new Thread(() -> {
                    SearchOuterLink searchOuterLink = new SearchOuterLink();
                    searchOuterLink.searchOuterLinkTask(e, blackList, whiteList, suffixList);
                }).start();
                e.setCrawlingState(1);
                senaClientSiteMapper.updateByPrimaryKeySelective(e);
            } else {
                log.info("参数错误");
            }


        });

    }


}
